package com.cw1.design1Single;

/**
 * @Author qiuMi
 * @description 单例模式 - 枚举
 * @Date 2022-09-27 21:49
 **/
public class SingleTest08 {
    public static void main(String[] args) {
        Single8 instance1 = Single8.SingleEnum.SINGLE_ENUM.getInstance();
        Single8 instance2 = Single8.SingleEnum.SINGLE_ENUM.getInstance();
        System.out.println(instance1 == instance2);
    }
}

/**
 * 单例模式 - 枚举
 */
class Single8{
    private Single8(){}

    /**
     * Java虚拟机会保证枚举类型不能被反射并且构造函数只被执行一次
     */
    public static enum SingleEnum{
        /**
         * 枚举
         */
        SINGLE_ENUM;
        private Single8 instance;

        SingleEnum(){
            instance = new Single8();
        }

        public Single8 getInstance(){
            return instance;
        }
    }
}

class Single88{
    private Single88(){}

    /**
     * 单例模式 - 枚举
     * Java虚拟机会保证枚举类型不能被反射并且构造函数只被执行一次
     */
    public enum Single88Enum{
        /**
         * Java虚拟机会保证枚举类型不能被反射并且构造函数只能被执行一次
         */
        SINGLE_88_ENUM;

        private Single88 instance;

        Single88Enum(){
            instance = new Single88();
        }

        public static Single88 getInstance(){
            return Single88Enum.getInstance();
        }
    }
}

